Quote:
Originally Posted by grail
I am not sure where you got this sentiment from?
|
The link, in my opinion, just ignores quoting and escaping rules altogether. It states that "
[[ is a new improved version of it" (referring to [), and only refers to quoting or escaping as something you do not need to do if you use [[ instead of [. For example, "
With [ the variable needs to be quoted". In my opinion, the link also gives the impression that quoting and escaping is something certain commands require.
Quoting and escaping rules are something you
have to do in shell scripts -- for external command or script execution if nothing else -- in all shells. The only exception I'm aware of is [[.
Between shells, there are only are minimal differences in escaping rules; I believe the quoting rules are identical. These rules are something a shell script writer should learn first, because they are applied everywhere (except for [[). If you apply the rules always, even when technically not required, your scripts will work regardless of input. If you fail to apply the rules, your scripts will break for some input.
I'm definitely not criticising the use of Bash over a POSIX shell. Indeed, I thought the example code David the H.
showed is very nice. It is a very good example in my opinion. I prefer Bash for shell scripts myself.
I only have a problem with recommending the use of [[. I seem to be the only one who believes [[ might reduce the incentive to learn the quoting and escaping rules, and that's fine. I understand that viewpoint. I just disagree. But don't worry, I'm not going to raise this issue again; I will just quietly keep avoiding [[ in my own example scripts, and ignoring the responses recommending replacing [ with [[.
I wonder if it would be worthwhile to write a short guide to quoting and escaping in shell scripts, with an introduction explaining its importance and prevalence, and point script writers to it whenever the quoting/escaping problems crop up?
Quote:
Originally Posted by konsolebox
Is it (set -f) generally implemented in all sh?
|
Yes,
-f (disable pathname expansion) and
-e (exit immediately if any nonzero exit status occurs) (and their counterparts
+f and
+e) are universally portable. They are defined in the POSIX shell spec (and thus guaranteed to not go away anytime soon), but also are implemented in all the shells I have access to, including the ancient sh in SunOS 5.10.
I avoid the other options, they're not as portable.
When using
set , just remember that a subshell will inherit the settings from its parent, and changing them won't change them in the parent. In a loop body, changing the setting once will persist over iterations. (Change it at the start of the first iteration, and it will stay that way for the rest of the loop.)